package me.zhengjie.modules.system.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.persistence.*
import jakarta.validation.constraints.NotNull
import me.zhengjie.common.base.BaseEntity
import me.zhengjie.common.utils.JacksonUtil
import org.hibernate.annotations.CreationTimestamp
import org.springframework.data.redis.core.RedisHash
import java.sql.Timestamp

/**
 * @author Kuki Wu
 * @date 2019-04-10
 */
@Entity
@Table(name = "sys_dict_detail")
@JsonTypeName("sysdictdetail")
@RedisHash("sysdictdetail")
data class DictDetail (
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "detail_id")
    @Schema(description = "ID", hidden = true)
    var id: @NotNull(groups = [Update::class]) Long? = null,

    // 字典id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dict_id")
    @Schema(description = "字典", hidden = true)
    var dict: Dict? = null,
    
    // 字典标签
    @Schema(description = "字典标签")
    var label: String? = null,

    // 字典值
    @Schema(description = "字典值")
    var value: String? = null,

    // 排序
    @Schema(description = "排序")
    var dictSort: Int? = 999
    
):BaseEntity(){
    annotation class Update

    companion object {
        fun fromJson(json: String): DictDetail? {
            val obj = JacksonUtil.parseObject(json, DictDetail::class.java)!!
            return when (obj) {
                is DictDetail -> obj
                else -> null
            }
        }
    }

    fun copy(source: DictDetail?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}